﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using EventPlanner.Validation;

namespace EventPlanner.Models
{
    public class Evento :EntityBase
    {        
        [Required]        
        public string Nombre { get; set; }
        [Display(ResourceType=typeof(EventPlanner.Resources.Global.Global),Name="lbl_Descripcion")]
        public string Descripcion { get; set; }
        
        [Display(ResourceType=typeof(EventPlanner.Resources.Global.Global), Name = "lbl_Fecha")]
        [DataType(DataType.Date)]
        
        [FechaValida(ErrorMessageResourceType=typeof(EventPlanner.Resources.Evento.Evento), ErrorMessageResourceName ="lbl_ValidacionFecha")]
        public DateTime Fecha { get; set; }
        [Display(ResourceType=typeof(EventPlanner.Resources.Global.Global), Name = "lbl_Hora")]
        [DataType(DataType.Time)]
        public DateTime Hora { get; set; } 
        public string Lugar { get; set; }
        public string Creador { get; set; }
        public int OrganizadorId { get; set; }
        public virtual Organizador Organizador { get; set; }
        public virtual ICollection<Participante> Participantes { get; set; }
        [Range(1, double.PositiveInfinity, ErrorMessageResourceType = typeof(EventPlanner.Resources.Evento.Evento), ErrorMessageResourceName ="lbl_ValidacionCupo")]
        public int Cupo { get; set; }

        public Participante GetParticipantePorUsuario(string name)
        {
            var r = from p in Participantes where p.Usuario == name select p;
            if (r.Count() == 0)
                return new Participante() { id = -1};
            else
                return r.First();
        }

        public bool CupoCompleto()
        {
            return Participantes.Count >= Cupo;
        }
    }
}